package com.ruoyi.source.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.source.api.domain.SrcStore;
import com.ruoyi.source.domain.vo.SrcStoreVo;
import com.ruoyi.source.service.ISrcStoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 仓储Controller
 * 
 * @author ruoyi
 * @date 2024-07-28
 */
@RestController
@RequestMapping("/store")
public class SrcStoreController extends BaseController {
    @Autowired
    private ISrcStoreService srcStoreService;

    /**
     * 查询仓储列表
     */
    @RequiresPermissions("source:store:list")
    @GetMapping("/list")
    public TableDataInfo list(SrcStore srcStore) {
        startPage();
        srcStore.setUserId(SecurityUtils.getUserId());
        List<SrcStore> list = srcStoreService.selectSrcStoreList(srcStore);
        return getDataTable(list);
    }

    /**
     * 导出仓储列表
     */
    @RequiresPermissions("source:store:export")
    @Log(title = "仓储", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SrcStore srcStore) {
        List<SrcStore> list = srcStoreService.selectSrcStoreList(srcStore);
        ExcelUtil<SrcStore> util = new ExcelUtil<SrcStore>(SrcStore.class);
        util.exportExcel(response, list, "仓储数据");
    }

    /**
     * 获取仓储详细信息
     */
    @RequiresPermissions("source:store:query")
    @GetMapping(value = "/{storeId}")
    public AjaxResult getInfo(@PathVariable("storeId") Long storeId) {
        return success(srcStoreService.selectSrcStoreByStoreId(storeId));
    }

    /**
     * 新增仓储
     */
    @RequiresPermissions("source:store:add")
    @Log(title = "仓储", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SrcStore srcStore) {
        return toAjax(srcStoreService.insertSrcStore(srcStore));
    }

    /**
     * 修改仓储
     */
    @RequiresPermissions("source:store:edit")
    @Log(title = "仓储", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SrcStore srcStore) {
        return toAjax(srcStoreService.updateSrcStore(srcStore));
    }

    /**
     * 删除仓储
     */
    @RequiresPermissions("source:store:remove")
    @Log(title = "仓储", businessType = BusinessType.DELETE)
	@DeleteMapping("/{storeIds}")
    public AjaxResult remove(@PathVariable Long[] storeIds) {
        return toAjax(srcStoreService.deleteSrcStoreByStoreIds(storeIds));
    }

    /**
     * 获取所有仓库列表
     * */
    @GetMapping("/get/store/list")
    public TableDataInfo getStoreList(){
        List<Map<String, Object>> storeList = srcStoreService.getStoreList();
        return getDataTable(storeList);
    }

    //更改仓库所属用户
    @GetMapping("/update/store/userIds")
    public void updateStoreUserIds(Long userId,Long[] storeIds){
        System.out.println("成功被调用");
        srcStoreService.updateStoreUserIds(userId,storeIds);
    }

    //获取用户所拥有的仓库id
    @GetMapping("/select/storeIds")
    public Long[] selectStoreIds(Long userId){
        List<SrcStore> list = srcStoreService.list(Wrappers.<SrcStore>lambdaQuery().select(SrcStore::getStoreId).eq(SrcStore::getUserId, userId));
        List<Long> storeIds = list.stream().map(SrcStore::getStoreId).collect(Collectors.toList());
        return storeIds.toArray(new Long[0]);
    }
}
